<?php

class GensetController extends Zend_Controller_Action
{

    protected $_genset = null;

    protected $_spekGenset = null;

    protected $_hargaSewa = null;
    
    protected $_konsumen = null;

    public function preDispatch()
    {
        $this->_genset = new Application_Model_Genset();
        $this->_spekGenset = new Application_Model_SpekGenset();
        $this->_hargaSewa = new Application_Model_HargaSewa();
        $this->_konsumen = new Application_Model_Konsumen();
    }

    public function init()
    {
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
       $genset = $this->_genset->getDataJoin();
       
       $this->view->genset = $genset;
    }

    public function addAction()
    {
        $form = new Application_Form_GensetForm();
        
        $form->getSubForm('spesifikasi')->removeDecorator('form');
        $form->getSubForm('spesifikasi')->removeDecorator('htmlTag');
        $formSpesifikasi = $form->getSubForm('spesifikasi');
        
        $form->getSubForm('hargasewa')->removeElement('daya_kva');
        $form->getSubForm('hargasewa')->removeElement('submit');
        $form->getSubForm('hargasewa')->removeElement('batal');
        
        $form->getSubForm('hargasewa')->getElement('harian')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('mingguan')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('bulanan')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('harian_bbm')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('mingguan_bbm')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('bulanan_bbm')->setAttrib('disabled', true);
        
        $form->getSubForm('hargasewa')->removeDecorator('form');
        $form->getSubForm('hargasewa')->removeDecorator('htmlTag');   
        $formHargasewa = $form->getSubForm('hargasewa');
        
        $this->view->spek = $formSpesifikasi;
        $this->view->serial_number = $form->getElement('serial_number_genset');
        $this->view->supplier = $form->getElement('id_supplier')->removeDecorator('DtDdWrapper');
        $this->view->submit = $form->getElement('submit')->setLabel('Add Data Genset');
        $this->view->harga = $formHargasewa;
        
        if($this->_request->isPost())
        {
            
            if($form->isValid($this->_request->getPost()))
            {
                
                
                /*Form Spesifikasi Genset*/
                $dataSpek = array(
                    'genset_model' => $formSpesifikasi->getValue('genset_model'),
                    'engine_type' => $formSpesifikasi->getvalue('engine_type'),
                    'engine_model' => $formSpesifikasi->getValue('engine_model'),
                    'daya_kva' => $formSpesifikasi->getValue('daya_kva'),
                    'no_silinder' => $formSpesifikasi->getValue('no_silinder'),
                    'no_stack' => $formSpesifikasi->getValue('no_stack'),
                    'daya_kw' => $formSpesifikasi->getValue('daya_kw'),
                    'bore_x_stroke' => $formSpesifikasi->getValue('bore_x_stroke'),
                    'piston_display' => $formSpesifikasi->getValue('piston_display'),
                    'fuel_consumption' => $formSpesifikasi->getValue('fuel_consumption'),
                    'oil_capacity' => $formSpesifikasi->getValue('oil_capacity'),
                    'length' => $formSpesifikasi->getValue('length'),
                    'width' => $formSpesifikasi->getValue('width'),
                    'height' => $formSpesifikasi->getValue('height'),
                    'weight' => $formSpesifikasi->getValue('weight'),
                    'fuel_tank_capacity' => $formSpesifikasi->getValue('fuel_tank_capacity'),
                    'jam_operasi' => $formSpesifikasi->getValue('jam_operasi')
                );
                
                /*Masukkan Data ke tabel_spek_engine dan tabel_harga_sewa*/
                $id_engine_genset = $this->_spekGenset->spekGensetBaru($dataSpek);
                $id_harga_sewa = $formHargasewa->getValue('id_harga_sewa');
                
                /*Form Genset*/
                $dataGenset = array(
                    'serial_number_genset' => $form->getValue('serial_number_genset'),
                    'id_supplier' => $form->getValue('id_supplier'),
                    'id_engine_genset' => $id_engine_genset,
                    'id_harga_sewa' => $id_harga_sewa,
                    'status_genset' => 'Belum Terpakai'
                );
                
                $this->_genset->gensetBaru($dataGenset);
                $this->_redirect('genset/index');
            } else {
                $form->populate($this->_request->getPost());
            }
        }
    }
    
    public function editAction()
    {
        $form = new Application_Form_GensetForm();
        
        $form->getSubForm('spesifikasi')->removeDecorator('form');
        $form->getSubForm('spesifikasi')->removeDecorator('htmlTag');
        $formSpesifikasi = $form->getSubForm('spesifikasi');
        
        $form->getSubForm('hargasewa')->removeElement('daya_kva');
        $form->getSubForm('hargasewa')->removeElement('submit');
        $form->getSubForm('hargasewa')->removeElement('batal');
        
        $form->getSubForm('hargasewa')->getElement('harian')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('mingguan')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('bulanan')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('harian_bbm')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('mingguan_bbm')->setAttrib('disabled', true);
        $form->getSubForm('hargasewa')->getElement('bulanan_bbm')->setAttrib('disabled', true);
        
        $form->getSubForm('hargasewa')->removeDecorator('form');
        $form->getSubForm('hargasewa')->removeDecorator('htmlTag');
        $formHargasewa = $form->getSubForm('hargasewa');
        
        $this->view->spek = $formSpesifikasi;
        $this->view->serial_number = $form->getElement('serial_number_genset');
        $this->view->supplier = $form->getElement('id_supplier');
        $this->view->submit = $form->getElement('submit')->setLabel('Save');
        $this->view->id = $form->getElement('id_genset');
        $this->view->harga = $formHargasewa;
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $dataSpek = array(
                    'genset_model' => $formSpesifikasi->getValue('genset_model'),
                    'engine_type' => $formSpesifikasi->getvalue('engine_type'),
                    'engine_model' => $formSpesifikasi->getValue('engine_model'),
                    'daya_kva' => $formSpesifikasi->getValue('daya_kva'),
                    'no_silinder' => $formSpesifikasi->getValue('no_silinder'),
                    'no_stack' => $formSpesifikasi->getValue('no_stack'),
                    'daya_kw' => $formSpesifikasi->getValue('daya_kw'),
                    'bore_x_stroke' => $formSpesifikasi->getValue('bore_x_stroke'),
                    'piston_display' => $formSpesifikasi->getValue('piston_display'),
                    'fuel_consumption' => $formSpesifikasi->getValue('fuel_consumption'),
                    'oil_capacity' => $formSpesifikasi->getValue('oil_capacity'),
                    'length' => $formSpesifikasi->getValue('length'),
                    'width' => $formSpesifikasi->getValue('width'),
                    'height' => $formSpesifikasi->getValue('height'),
                    'weight' => $formSpesifikasi->getValue('weight'),
                    'fuel_tank_capacity' => $formSpesifikasi->getValue('fuel_tank_capacity'),
                    'jam_operasi' => $formSpesifikasi->getValue('jam_operasi')
                );
                $id_spek = $formSpesifikasi->getValue('id_engine_genset');
                
                $id_harga = $formHargasewa->getValue('id_harga_sewa');
            
                $this->_spekGenset->spekGensetUpdate($dataSpek, $id_spek);
            
                $dataGenset = array(
                    'serial_number_genset' => $form->getValue('serial_number_genset'),
                    'id_supplier' => $form->getValue('id_supplier'),
                    'id_harga_sewa' => $id_harga
                );
                $id_genset = $form->getValue('id_genset');
                $this->_genset->gensetUpdate($dataGenset, $id_genset);
                $this->_redirect('genset/index');
            }
        } else {
            $id = $this->_getParam('id');
            if($id>0) {
                $data = $this->_genset->getDataJoinId($id);
                $form->populate($data);
                
                $daya = new Application_Model_HargaSewa();
                $daya_kva = $data['daya_kva'];
                
                $data = $daya->getRow($daya_kva, 'Dengan BBM');
                $cols = $daya->info(Zend_Db_Table_Abstract::COLS);
                $col = array();
                
                foreach($cols as $cols)
                {
                    $col[] = $cols;
                }
                $harga = array();
                $i = 0;
                foreach($data as $data)
                {
                    $colomn = $col[$i];
                    $harga[$colomn.'_bbm'] = $data;
                    $i++;
                }
                $formHargasewa->populate($harga);
            }
                
        }
    }

    public function deleteAction()
    {
        $form = new Application_Form_DeleteForm;
        
        $form->removeElement('id_departemen');
        $form->addElement('hidden', 'id_genset', 'int');
        $form->addElement('hidden', 'id_engine_genset', 'int');
        $form->addElement('hidden', 'id_harga_sewa', 'int');
        
        $form->setAction("genset/delete");
        
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {
            $del = $this->getRequest()->getPost('del1');
            if($del == 'Ya')
            {
                $this->_genset->gensetDelete($this->getRequest()->getPost('id_genset'));
                $this->_spekGenset->spekGensetDelete($this->getRequest()->getPost('id_engine_genset'));  
                $this->_redirect("genset/index");
            }
            else {
                $this->_redirect("genset/index");
            }
        } else {
           $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $data = $this->_genset->getDataJoinId($id);
                $form->populate($data);
            }
        }
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        if ($id != null) {
            
            $genset = $this->_genset->getDataJoinId($id);
            
            $daya = new Application_Model_HargaSewa();
            $daya_kva = $genset['daya_kva']; 
            $data = $daya->getHarga($daya_kva);
            
            if($genset == null)
            {
                $this->view->notFound = '<div class="notfound">Data Tidak Ditemukan</div>';
            } else {
                $id_konsumen = $genset['id_konsumen'];
                $location = $this->_konsumen->getDataKonsumen($id_konsumen);
                foreach($location as $location);
                $this->view->genset = $genset;
                $this->view->daya = $data;
                $this->view->location = $location;
            }
            
        } else {
            $this->_redirect('genset/index');
        }
    }


}





